package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.model.AirConditioner;
import com.kust.smarthome.web.model.DeviceState;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IAirConditionerService;
import com.kust.smarthome.web.service.IDeviceStateService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/airConditioner")
@Api(value = "空调相关操作", tags = {"device-tag"})
public class AirConditionerController {
    @Resource
    private IAirConditionerService airConditionerService;

    @Resource
    private IDeviceStateService deviceStateService;

    @Resource
    private IUserService userService;

    /**
     * 通过用户信息获取对应设备列表
     * @param principal Security框架中获取登录用户的详细信息
     * @return 对应用户的空调设备表
     */
    @GetMapping("")
    @ApiOperation(value = "获取空调信息", notes = "通过用户信息获取空调信息")
    public List<AirConditioner> list(Principal principal) {
        User u = userService.getByUsername(principal.getName());
        return airConditionerService.getByUserId(u.getId());
    }

    @GetMapping("/getById")
    @ApiOperation(value = "getById", notes = "通过id获取对应设备")
    public AirConditioner getById(Integer id) {
        return airConditionerService.getById(id);
    }

    /**
     * 客户端请求一次开关空调,传给后端id,若该id对应的空调的状态为1(开)则将状态变为0,反之变为1
     * 同时,在状态表中插入对应状态
     * @param id curtain.getId()
     * @return 返回所有的空调属性,前端js代码成功收到响应之后将vue中的数据重新绑定即可
     */
    /*@GetMapping("/update")
    @Transactional
    @ApiOperation(value = "开关空调", notes = "用户通过空调id开关空调操作")
    public List<AirConditioner> updateById(Principal principal, Integer id) {
        AirConditioner airConditioner = airConditionerService.getById(id);
        Integer state = 1;
        if (airConditioner.getState() == 1) {
            airConditioner.setState(0);
            state = 0;
        } else if (airConditioner.getState() == 0) {
            airConditioner.setState(1);
            state = 1;
        }

        airConditionerService.updateById(airConditioner);
        DeviceState deviceState = new DeviceState()
                .setDeviceId(id)
                .setTypeId(6)
                .setIsUpdate(0);
        //更改状态通知
        QueryWrapper<DeviceState> queryD = new QueryWrapper<>();
        queryD.eq("device_id",id);
        queryD.eq("type_id",6);
        deviceStateService.update(deviceState,queryD);

        User user = userService.getByUsername(principal.getName());

        return airConditionerService.getByUserId(user.getId());
    }*/

    @GetMapping("/update")
    @Transactional
    @ApiOperation(value = "开空调操作", notes = "提供空调上传状态")
    public List<DeviceState> updateById(Integer id, Integer state,String content) {
        AirConditioner airConditioner = airConditionerService.getById(id);
        airConditioner.setState(state);
        airConditioner.setContent(content);
        airConditionerService.updateById(airConditioner);
        return airConditionerService.findOperationList(id,6);
    }

    @GetMapping("/done")
    @ApiOperation(value = "改变操作状态", notes = "前端提供id")
    public boolean changeUpdate(Integer id){
        airConditionerService.setupdate(id);
        return true;
    }


    /**
     * 网页端用户添加空调,方法中初始化一个空调对象并插入到数据表中
     * 若对空调数量有限制,前端可以添加一个判定条件,是否执行异步请求,同时后端也会做一次判定
     * 最大数量先定为3
     * @return 返回插入之后的空调列表
     */
    @GetMapping("/add")
    @Transactional
    @ApiOperation(value = "添加空调", notes = "用户提供空调id进行添加空调操作")
    public List<AirConditioner> addCurtain(Principal principal, DeviceVo deviceVo){

        User u = userService.getByUsername(principal.getName());

        List<AirConditioner> list = airConditionerService.getByUserId(u.getId());

        LocalDateTime now = LocalDateTime.now();

        if (list.size() < 3) {
            AirConditioner airConditioner = new AirConditioner()
                    .setId(deviceVo.getId())
                    .setUrl("")
                    .setContent("这是一台空调")
                    .setState(0)
                    .setCreated(now)
                    .setUserId(u.getId())
                    .setRoomId(deviceVo.getRoomId());
            airConditionerService.insert(airConditioner);
            list.add(airConditioner);
        }

        DeviceState deviceState = new DeviceState()
                .setDeviceId(deviceVo.getId())
                .setTypeId(6)
                .setIsUpdate(0);
        //插入状态变更通知
        deviceStateService.insert(deviceState);

        return list;
    }

    /**
     * 删除一个空调对象操作
     * 同时要删除其所对应的状态表
     * @param id 由客户端传入一个id
     * @return 当后端处理完删除操作之后返回当前数据库中的空调对象列表
     */
    @GetMapping("/deleteById")
    @Transactional
    @ApiOperation(value = "删除空调", notes = "用户通过空调id删除空调")
    public List<AirConditioner> deleteById(Principal principal, Integer id) {
        AirConditioner airConditioner = airConditionerService.getById(id);
        airConditioner.setUserId(-1);
        airConditionerService.updateById(airConditioner);
        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id",airConditioner.getId());
        query.eq("type_id",6);
        deviceStateService.remove(query);

        User u = userService.getByUsername(principal.getName());
        return airConditionerService.getByUserId(u.getId());
    }
}
